home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / HyperCuber Source / HyperCuber 2.0 Source.sit / HyperCuber 2.0 Source / Keys.cp < prev    next >
Text File  |  1994-04-05  |  7KB  |  188 lines

  1. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. //| Keys.cp
  3. //|
  4. //| This contains procedures relating to the use
  5. //| of key controls in HyperCuber.
  6. //|_________________________________________________________
  7.  
  8. #include "Keys.h"
  9.  
  10. #include <string.h>
  11.  
  12.  
  13. //======================== Prototypes ========================\\
  14.  
  15. void DrawKey(short key_code, short modifiers, short h, short v);
  16. void DrawModifiers(short modifiers, short h, short v);
  17. void DrawKeySymbol(short pict_id, short& h, short v);
  18. void GetKeyString(short keycode, char *key_name);
  19.  
  20.  
  21.  
  22. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. //| Procedure DrawKey
  24. //|
  25. //| Purpose: Draws a text-and-icons description of a key, given the key code
  26. //|          and the modifiers.  The text description of the key will be
  27. //|          drawn to the right of (h, v) and the modifiers will be drawn to
  28. //|          the left of (h, v).
  29. //|
  30. //| Parameters: key_code: the key code of the key
  31. //|             modifers: the modifiers associated with the keyDown event
  32. //|             h:        the horizontal position to draw
  33. //|             v:        the vertical position to draw
  34. //|______________________________________________________________________________
  35.  
  36. void DrawKey(short key_code, short modifiers, short h, short v)
  37. {
  38.  
  39. #define COMMAND_PICT    130
  40. #define OPTION_PICT        131
  41. #define SHIFT_PICT        132
  42. #define CONTROL_PICT    133
  43. #define CAPS_LOCK_PICT    134
  44.  
  45.     char key_string[10];
  46.     GetKeyString(key_code, key_string);            //  Get text description of the key
  47.     MoveTo(h, v);
  48.     DrawString(CtoPstr(key_string));            //  Draw the text description
  49.     
  50.     DrawModifiers(modifiers, h, v-9);            //  Draw the modifiers
  51.  
  52. }    //==== DrawKey() ====\\
  53.  
  54.  
  55.  
  56. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  57. //| Procedure DrawModifiers
  58. //|
  59. //| Purpose: Draws the modifiers, given the modifiers word.  This procedure
  60. //|          draws to the left of (h, v).
  61. //|
  62. //| Parameters: modifers: the modifiers word
  63. //|             h:        the horizontal position to draw
  64. //|             v:        the vertical position to draw
  65. //|______________________________________________________________________________
  66.  
  67. void DrawModifiers(short modifiers, short h, short v)
  68. {
  69.  
  70.     if (modifiers & cmdKey)
  71.         DrawKeySymbol(COMMAND_PICT, h, v);
  72.     
  73.     if (modifiers & optionKey)
  74.         DrawKeySymbol(OPTION_PICT, h, v);
  75.  
  76.     if (modifiers & shiftKey)
  77.         DrawKeySymbol(SHIFT_PICT, h, v);
  78.  
  79.     if (modifiers & controlKey)
  80.         DrawKeySymbol(CONTROL_PICT, h, v);
  81.  
  82.     if (modifiers & alphaLock)
  83.         DrawKeySymbol(CAPS_LOCK_PICT, h, v);
  84.  
  85. }    //==== DrawModifiers() ====\\
  86.  
  87.  
  88.  
  89. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  90. //| Procedure DrawKeySymbol
  91. //|
  92. //| Purpose: Draw the symbol for a modifier key.
  93. //|
  94. //| Parameters: pict_id: the id of the PICT for the symbol
  95. //|             h:       the horizontal position of the cursor (this procedure
  96. //|                      moves the cursor back in preparation for the next draw)
  97. //|             v:       the vertical position of the cursor
  98. //|______________________________________________________________________________
  99.  
  100. void DrawKeySymbol(short pict_id, short& h, short v)
  101. {
  102.  
  103.     PicHandle picture = GetPicture(pict_id);            //  Get symbol picture
  104.     Rect pict_rect = (*picture)->picFrame;                //  Find size of symbol
  105.     h -= pict_rect.right - pict_rect.left + 2;            //  Move cursor back by width of symbol
  106.     OffsetRect(&pict_rect, h, v);                        //  Find location to draw symbol
  107.     DrawPicture(picture, &pict_rect);                    //  Draw symbol
  108.  
  109. }    //==== DrawKeySymbol() ====\\
  110.  
  111.  
  112.  
  113. //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  114. //| Procedure GetKeyString
  115. //|
  116. //| Purpose: Get a string version of a key.
  117. //|
  118. //| Parameters: keycode:  the key
  119. //|             key_name: receives the string
  120. //|______________________________________________________________________________
  121.  
  122. void GetKeyString(short keycode, char *key_name)
  123. {
  124.  
  125.     static    long state = 0;
  126.  
  127.     char lowbyte = keycode;                        //  Get key code as a byte
  128.     
  129.     switch(lowbyte)                                //  Handle strange cases
  130.         {
  131.         case ' ':            strcpy(key_name, "space"); break;
  132.         case KeyHome:        strcpy(key_name, "home"); break;
  133.         case KeyEnd:        strcpy(key_name, "end"); break;
  134.         case KeyPageUp:        strcpy(key_name, "pgup"); break;
  135.         case KeyPageDown:    strcpy(key_name, "pgdn"); break;
  136.         case KeyEscape:        strcpy(key_name, "esc"); break;
  137.         case KeyClear:        strcpy(key_name, "clear"); break;
  138.         case KeyHelp:        strcpy(key_name, "help"); break;
  139.         case KeyFwdDelete:    strcpy(key_name, "fwdel"); break;
  140.         case KeyLeftCursor:    strcpy(key_name, "left"); break;
  141.         case KeyRightCursor:strcpy(key_name, "right"); break;
  142.         case KeyUpCursor:    strcpy(key_name, "up"); break;
  143.         case KeyDownCursor:    strcpy(key_name, "down"); break;
  144.         case KeyReturn:        strcpy(key_name, "return"); break;
  145.         case KeyDelete:        strcpy(key_name, "delete"); break;
  146.         case KeyEnter:        strcpy(key_name, "enter"); break;
  147.         case KeyF1:            strcpy(key_name, "F1"); break;
  148.         case KeyF2:            strcpy(key_name, "F2"); break;
  149.         case KeyF3:            strcpy(key_name, "F3"); break;
  150.         case KeyF4:            strcpy(key_name, "F4"); break;
  151.         case KeyF5:            strcpy(key_name, "F5"); break;
  152.         case KeyF6:            strcpy(key_name, "F6"); break;
  153.         case KeyF7:            strcpy(key_name, "F7"); break;
  154.         case KeyF8:            strcpy(key_name, "F8"); break;
  155.         case KeyF9:            strcpy(key_name, "F9"); break;
  156.         case KeyF10:        strcpy(key_name, "F10"); break;
  157.         case KeyF11:        strcpy(key_name, "F11"); break;
  158.         case KeyF12:        strcpy(key_name, "F12"); break;
  159.         case KeyF13:        strcpy(key_name, "F13"); break;
  160.         case KeyF14:        strcpy(key_name, "F14"); break;
  161.         case KeyF15:        strcpy(key_name, "F15"); break;
  162.         case KeyPadEquals:    strcpy(key_name, "[=]"); break;
  163.         case KeyPadDiv:        strcpy(key_name, "[/]"); break;
  164.         case KeyPadMult:    strcpy(key_name, "[*]"); break;
  165.         case KeyPadPlus:    strcpy(key_name, "[+]"); break;
  166.         case KeyPadMinus:    strcpy(key_name, "[-]"); break;
  167.         case KeyPadDot:        strcpy(key_name, "[.]"); break;
  168.         case KeyPad0:        strcpy(key_name, "[0]"); break;
  169.         case KeyPad1:        strcpy(key_name, "[1]"); break;
  170.         case KeyPad2:        strcpy(key_name, "[2]"); break;
  171.         case KeyPad3:        strcpy(key_name, "[3]"); break;
  172.         case KeyPad4:        strcpy(key_name, "[4]"); break;
  173.         case KeyPad5:        strcpy(key_name, "[5]"); break;
  174.         case KeyPad6:        strcpy(key_name, "[6]"); break;
  175.         case KeyPad7:        strcpy(key_name, "[7]"); break;
  176.         case KeyPad8:        strcpy(key_name, "[8]"); break;
  177.         case KeyPad9:        strcpy(key_name, "[9]"); break;
  178.         
  179.         default:
  180.             Handle kchr = GetResource('KCHR', 0);                //  Handle normal characters
  181.             long charcode = KeyTrans(*kchr, keycode, &state);
  182.             key_name[0] = charcode;                                //  Make a 1-character string
  183.             key_name[1] = 0;
  184.             
  185.         }
  186.     
  187. }    //==== GetKeyString() ====\\
  188.